home *** CD-ROM | disk | FTP | other *** search
/ Aminet 52 / Aminet 52 (2002)(GTI - Schatztruhe)[!][Dec 2002].iso / Aminet / util / moni / Sysmon120a.lha / sysmon / src / sysinfo / UpTime.c < prev   
C/C++ Source or Header  |  2001-08-03  |  7KB  |  289 lines

  1. /*
  2. **    $RCSfile: Uptime.c,v $
  3. **    $Filename: Uptime.c $
  4. **    $Revision: 1.0 $
  5. **    $Date: $
  6. **
  7. **    Modified by Etienne Vogt from the SysInfo.library example by Petri Nordlund
  8. **    This file is public domain.
  9. */
  10.  
  11. /*
  12.  * This is a fairly complete uptime-program, based on the example provided
  13.  * with the original Executive-based SysInfo.library, which has been modified
  14.  * for use with Sysmon. 
  15.  * It supports multiuser.library support to show the number of users currently
  16.  * logged in.
  17.  */
  18.  
  19. #include <exec/memory.h>
  20. #include <exec/execbase.h>
  21. #include <dos/datetime.h>
  22. #include <devices/timer.h>
  23. #define __USE_SYSBASE
  24. #include <proto/exec.h>
  25. #include <proto/dos.h>
  26. #include <proto/timer.h>
  27. #include <proto/multiuser.h>
  28. #include <libraries/multiuser.h>
  29. #include "SysInfo.h"
  30. #include "SysInfo_protos.h"
  31. #include "SysInfo_pragmas.h"
  32. #include "/sysmon.h"
  33. #include "/sysmon_protos.h"
  34. #include "/sysmon_pragmas.h"
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <ctype.h>
  39.  
  40.  
  41. /* PROTOTYPES */
  42. int main(int argc, char **argv);
  43. static void cleanexit(ULONG rc);
  44. static void AddUser(ULONG user, UWORD *uids, ULONG *nusers);
  45. static ULONG CountTasks(void);
  46. static void GetTime(void);
  47. static void Users(void);
  48. static void LoadAverages(void);
  49. static void Uptime(void);
  50. static ULONG EClockDivide(struct EClockVal *numerator, ULONG denominator);
  51.  
  52.  
  53. /* VARIABLES */
  54. struct muBase *muBase = NULL;
  55. struct SysInfo *si = NULL;
  56. struct Library *SysInfoBase = NULL;
  57. extern struct ExecBase *SysBase;
  58. extern struct DosLibrary *DOSBase;
  59. struct SysmonBase *SysmonBase = NULL;
  60. struct Device *TimerBase = NULL;
  61. struct timerequest myTimeReq;
  62.   
  63. static UBYTE version[] = "$VER: Uptime 1.0 (3.8.2001)";
  64.  
  65. int main(int argc, char **argv)
  66. {
  67.   if ((SysmonBase = (struct SysmonBase *)OpenLibrary("sysmon.library",1)) == NULL)
  68.   { puts("Can't open sysmon.library.");
  69.     cleanexit(RETURN_FAIL);
  70.   }
  71.  
  72.   if (!(SysInfoBase = OpenLibrary(SYSINFONAME, SYSINFOVERSION)))
  73.   { puts("Can't open SysInfo.library.");
  74.     cleanexit(RETURN_FAIL);
  75.   }
  76.  
  77.   /* Initialize SysInfo.library, this will make the connection to the
  78.    * server-process and allocate the SysInfo-structure. */
  79.   if (!(si = InitSysInfo()))
  80.   { puts("Can't initialize SysInfo.");
  81.     cleanexit(RETURN_FAIL);
  82.   }
  83.  
  84.   if (OpenDevice(TIMERNAME, UNIT_ECLOCK, (struct IORequest *)&myTimeReq, 0L))
  85.   { printf("Can't open %s\n", TIMERNAME);
  86.     cleanexit(RETURN_FAIL);
  87.   }
  88.   else TimerBase = myTimeReq.tr_node.io_Device;
  89.  
  90.   /* If this fails, we'll just output "1 user" */
  91.   muBase = (struct muBase *) OpenLibrary(MULTIUSERNAME, MULTIUSERVERSION);
  92.  
  93.   GetTime();
  94.   printf(", ");
  95.   Uptime();
  96.   printf(", ");
  97.   Users();
  98.   printf(", ");
  99.   LoadAverages();
  100.   printf("\n");
  101.  
  102.   cleanexit(RETURN_OK);
  103. }
  104.  
  105.  
  106. /*
  107.  * Exit. Free everything.
  108.  */
  109. static void cleanexit(ULONG rc)
  110. {
  111.   if (muBase) CloseLibrary((struct Library *)muBase);
  112.   if (TimerBase) CloseDevice((struct IORequest *)&myTimeReq);
  113.  
  114.   /* Free SysInfo.library */
  115.   if (si) FreeSysInfo(si);
  116.   if (SysInfoBase) CloseLibrary(SysInfoBase);
  117.   if (SysmonBase) CloseLibrary((struct Library *)SysmonBase);
  118.   exit(rc);
  119. }
  120.  
  121.  
  122. /*
  123.  * Print system time
  124.  */
  125. static void GetTime(void)
  126. {
  127.   struct DateTime dtime;
  128.   char timestr[LEN_DATSTRING+1];
  129.  
  130.   DateStamp(&dtime.dat_Stamp);
  131.  
  132.   dtime.dat_Format = FORMAT_DOS;
  133.   dtime.dat_Flags = 0;
  134.   dtime.dat_StrDay = NULL;
  135.   dtime.dat_StrDate = NULL;
  136.   dtime.dat_StrTime = timestr;
  137.  
  138.   DateToStr(&dtime);
  139.  
  140.   printf(timestr);
  141. }
  142.  
  143.  
  144. /*
  145.  * Print number of users (from Multiuser if available)
  146.  */
  147. static void Users(void)
  148. {
  149.   ULONG nusers = 1;
  150.  
  151.   if(muBase)
  152.   { ULONG numtasks;
  153.     UWORD *uids;
  154.  
  155.     smLockTaskTable(LTTF_READ);
  156.  
  157.   /* Count the number of tasks currently in system */
  158.     numtasks = CountTasks();
  159.  
  160.   /* Allocate memory for each task */
  161.     if (uids=AllocVec(numtasks*sizeof(UWORD),MEMF_CLEAR|MEMF_PUBLIC))
  162.     { struct TaskInfo *tinfo = NULL;
  163.  
  164.     /* Find out how many different uids there are */
  165.  
  166.       nusers = 0;
  167.       while (tinfo = smNextTaskInfo(tinfo)) AddUser(muGetTaskOwner(tinfo->ti_Task), uids, &nusers);
  168.       FreeVec(uids);
  169.     }
  170.  
  171.     smUnLockTaskTable(LTTF_READ);
  172.   }
  173.  
  174.   if ((nusers>1) || (nusers==0)) printf("%d users", nusers);
  175.   else printf("%d user",nusers);
  176. }
  177.  
  178.  
  179. static void AddUser(ULONG user, UWORD *uids, ULONG *nusers)
  180. {
  181.   UWORD uid;
  182.   BOOL found = FALSE;
  183.   ULONG i;
  184.  
  185.   if (user)
  186.   { uid = user>>16;
  187.     for (i=0; !found && (i<*nusers); i++) found = (uids[i] == uid);
  188.     if (!found) uids[(*nusers)++] = uid;
  189.   }
  190. }
  191.  
  192. static ULONG CountTasks(void)
  193. { ULONG i = 0;
  194.   struct TaskInfo *tinfo = NULL;
  195.  
  196.   while (tinfo = smNextTaskInfo(tinfo)) i++;
  197.  
  198.   return i;
  199. }
  200.  
  201.  
  202. /*
  203.  * Print uptime from timer.device EClock.
  204.  */
  205. static void Uptime(void)
  206. { struct EClockVal UpTime;
  207.   ULONG EClockRate;
  208.   ULONG days, hrs, mins;
  209.  
  210.   EClockRate = ReadEClock(&UpTime);
  211.   
  212.   mins = EClockDivide(&UpTime, 60*EClockRate);
  213.   days = mins / (24*60);
  214.   mins %= 24*60;
  215.   hrs = mins / 60;
  216.   mins %= 60;
  217.  
  218.   if (days || hrs || mins) printf("up ");
  219.  
  220.   if (days > 0)
  221.   { if (days > 1) printf("%d days ", days);
  222.     else printf("%d day ", days);
  223.   }
  224.  
  225.   if (hrs > 0) printf("%d:%02d", hrs, mins);
  226.   else
  227.   { if ((mins > 1) || (mins == 0)) printf("%d mins", mins);
  228.     else printf("%d min", mins);
  229.   }
  230. }
  231.  
  232. static ULONG EClockDivide(struct EClockVal *numerator, ULONG denominator)
  233. { int bits;
  234.   ULONG d1,d2,d3;
  235.  
  236.   d1 = 0x0;
  237.   d2 = numerator->ev_hi;
  238.   d3 = numerator->ev_lo;
  239.  
  240.   for (bits = 0; bits < 64; bits++)
  241.   { /* now left-shift d1,d2,d3 */
  242.     d1 <<= 1;
  243.     if (d2 & 0x80000000) d1 |= 0x1;
  244.     d2 <<= 1;
  245.     if (d3 & 0x80000000) d2 |= 0x1;
  246.     d3 <<= 1;
  247.  
  248.     if (d1 >= denominator) 
  249.     { d1 -= denominator;
  250.       d3 |= 0x1;
  251.     }
  252.   }
  253.  
  254.   /* fill in the remainder */
  255.   numerator->ev_lo = d1;
  256.  
  257.   /* return the quotient, or the low part of it, at least */
  258.   return d3;
  259. }
  260.  
  261.  
  262. /*
  263.  * Print load averages
  264.  */
  265. static void LoadAverages(void)
  266. { struct SI_LoadAverage load;      /* This will be filled by GetLoadAverage() */
  267.  
  268.   GetLoadAverage(si, &load);      /* Ask SysInfo.library for current load averages */
  269.  
  270.   printf("load:");
  271.  
  272.   switch (si->loadavg_type)
  273.   { case LOADAVG_FIXEDPNT:     /* Convert fixed point values to floating point values */
  274.       if (si->loadavg_time1) printf(" %.2f",(float) load.lavg_fixed.load1 / (float) si->fscale);
  275.       else printf(" N/A");
  276.  
  277.       if (si->loadavg_time2) printf(" %.2f",(float) load.lavg_fixed.load2 / (float) si->fscale);
  278.       else printf(" N/A");
  279.  
  280.       if (si->loadavg_time3) printf(" %.2f",(float) load.lavg_fixed.load3 / (float) si->fscale);
  281.       else printf(" N/A");
  282.  
  283.       break;
  284.  
  285.     default:              /* Load average type is not supported */
  286.       printf("-");
  287.   }
  288. }
  289.